﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsEmailSettingsTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// EmailSettings Business Object
    /// </summary>

    public class EmailSettings : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected EmailSettings() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// EmailFor.
        /// </summary>
        public string EmailFor
        {
            get { return _emailFor; }
            set
            {
                if (_emailFor != value)
                {
                    if (OnPropertyChanging("EmailFor", _emailFor, value))
                    {
                        string oldValue = _emailFor;
                        _emailFor = value;
                        OnPropertyChanged("EmailFor", oldValue, value);
                    }
                }
            }
        }  private string _emailFor = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty EmailSettings object.
        /// </summary>
        public static EmailSettings CreateNew()
        {
            EmailSettings obj = new EmailSettings();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static EmailSettings CreateNew(EmailSettingsCollection collection)
        {
            EmailSettings obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetEmailSettingsByUserID

        /// <summary>
        /// Returns a collection of EmailSettings.
        /// </summary>
        public static EmailSettingsCollection GetEmailSettingsByUserID(int userid)
        {
            EmailSettingsTableAdapter adapter = new EmailSettingsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsEmailSettings.EmailSettingsDataTable tbl = adapter.GetData(userid);

            EmailSettingsCollection list = new EmailSettingsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsEmailSettings.EmailSettingsRow row in tbl.Rows)
                {
                    EmailSettings emailsettings = new EmailSettings();
                    RelationalToObject(emailsettings, row);
                    list.Add(emailsettings);
                }
            }

            return (list);
        }

        #endregion

        #region DeleteEmailSettingsByUserID

        /// <summary>
        /// Updates the specified EmailSettings.
        /// </summary>
        public static void Delete(int userid)
        {
            EmailSettingsTableAdapter adapter = new EmailSettingsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            adapter.Delete(userid);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified EmailSettings.
        /// </summary>
        public static void Update(EmailSettings obj)
        {
            bool updateEmailSettings = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateEmailSettings = (obj.ObjState != ObjectState.Unchanged);

                if (!updateEmailSettings)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsEmailSettings.EmailSettingsDataTable tbl = null;
            if (updateEmailSettings)
            {
                EmailSettingsTableAdapter adapter = new EmailSettingsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsEmailSettings.EmailSettingsDataTable();
                dsEmailSettings.EmailSettingsRow row = tbl.NewEmailSettingsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("UserName", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("EmailSettings: UserName={0}, EmailFor={1}, {2}", UserID, EmailFor));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class EmailSettingsCollection : List<EmailSettings>
    {
        #region Types

        public class UserComparer : IComparer<EmailSettings>
        {
            public int Compare(EmailSettings ef1, EmailSettings ef2)
            {
                return (String.Compare(ef1.EmailFor, ef2.EmailFor));
            }
        }

        #endregion

        #region Public Methods

        #region FindByUserID

        public EmailSettings FindByUserID(int userid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].UserID == userid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
